package com.example.demo.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.QueryBaseParam;
import com.example.demo.domain.entity.TrainClass;
import com.example.demo.domain.entity.TrainClassHomework;
import com.example.demo.domain.model.TrainClassForm;
import com.example.demo.enums.TrainClassStatusEnum;
import com.example.demo.param.TrainClassParam;
import com.example.demo.service.TrainClassService;
import com.example.demo.mapper.TrainClassMapper;
import com.example.demo.utils.CommonUtil;
import com.example.demo.utils.UuidUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author liguang
* @description 针对表【train_class(培训系统-班级)】的数据库操作Service实现
* @createDate 2023-10-23 22:42:58
*/
@Service
@Slf4j
public class TrainClassServiceImpl extends ServiceImpl<TrainClassMapper, TrainClass>
    implements TrainClassService{


    private static final Integer INVITE_CODE_UUID_TIMES = 3;

    @Resource
    private TrainClassMapper trainClassMapper;


    @Override
    public Boolean insertTrainClass(TrainClassForm form) {
        String inviteCode = getInviteCode();
        if (StringUtils.isEmpty(inviteCode)){
            log.info("创建培训班生成邀请码失败：{}", JSONUtil.toJsonStr(form));
            return false;
        }
        TrainClass trainClass = new TrainClass();
        trainClass.setName(form.getName());
        trainClass.setNotes(form.getNotes());
        trainClass.setInviteCode(inviteCode);
        trainClass.setCreatorId(form.getCreator());
        trainClass.setBelongTeacherId(form.getBelongTeacherId());
        trainClass.setCompanyId(form.getCompanyId());
        trainClass.setStatus(TrainClassStatusEnum.ENABLE.getKey());
        return trainClassMapper.insert(trainClass) > 0 ;
    }


    @Override
    public List<TrainClass> getByIds(List<Long> classIds) {
        QueryWrapper<TrainClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", classIds);
        queryWrapper.eq("status", TrainClassStatusEnum.ENABLE.getKey());
        return trainClassMapper.selectList(queryWrapper);
    }

    @Override
    public Page<TrainClass> getByIdsAndPage(List<Long> classIds, QueryBaseParam param) {
        QueryWrapper<TrainClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", classIds);
        queryWrapper.eq("status", TrainClassStatusEnum.ENABLE.getKey());
        queryWrapper.orderByDesc("create_time");
        Page<TrainClass> page = new Page<>(param.getPageNumber(), param.getPageSize());
        return trainClassMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Page<TrainClass> queryTrainClass(TrainClassParam param) {
        QueryWrapper<TrainClass> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(param.getName())){
            queryWrapper.like("name", param.getName());
        }
        if (param.getCreateStartTime() != null ) {
            queryWrapper.ge("create_time", param.getCreateStartTime());
        }
        if (param.getCreateEndTime() != null) {
            queryWrapper.le("create_time", param.getCreateEndTime());
        }
        if (param.getStatus() != null) {
            queryWrapper.eq("status", param.getStatus());
        }else {
            queryWrapper.eq("status", TrainClassStatusEnum.ENABLE.getKey());
        }

        if (StringUtils.isNotEmpty(param.getInviteCode())){
            queryWrapper.eq("invite_code", param.getInviteCode());
        }

        if (CommonUtil.isNotNullId(param.getBelongTeacherId())){
            queryWrapper.eq("belong_teacher_id", param.getBelongTeacherId());
        }
        if (CommonUtil.isNotNullId(param.getCompanyId())){
            queryWrapper.eq("company_id", param.getCompanyId());
        }

        if (StringUtils.isEmpty(param.getOrderBy()) && StringUtils.isEmpty(param.getOrderName())){
            queryWrapper.orderByDesc("create_time");
        }

        Page<TrainClass> page = new Page<>(param.getPageNumber(), param.getPageSize());
        return trainClassMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<TrainClass> queryAllTrainClassByCompanyId(Long companyId) {
        QueryWrapper<TrainClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", companyId);
        queryWrapper.eq("status", TrainClassStatusEnum.ENABLE.getKey());
        queryWrapper.orderByDesc("create_time");
        return trainClassMapper.selectList(queryWrapper);
    }

    @Override
    public TrainClass queryByInviteCode(String inviteCode) {
        QueryWrapper<TrainClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("invite_code", inviteCode);
        return trainClassMapper.selectOne(queryWrapper);
    }

    @Override
    public List<TrainClass> queryTrainClassByNameAndUserId(String name, Long userId) {
        QueryWrapper<TrainClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("belong_teacher_id", userId );
        queryWrapper.eq("status", TrainClassStatusEnum.ENABLE.getKey());
        queryWrapper.orderByDesc("create_time");
        return trainClassMapper.selectList(queryWrapper);
    }


    public String getInviteCode(){
        try{
            int time = 1;
            while (time < INVITE_CODE_UUID_TIMES){
                String uuid = UuidUtil.getUuid();
                TrainClassParam param = new TrainClassParam();
                param.setInviteCode(uuid);
                TrainClass trainClass = queryByInviteCode(uuid);
                if (trainClass == null) {
                    return uuid;
                }
                time++;
            }
        }catch (Exception e){
            log.error("");
        }
        return "";
    }

}




